package com.jackstraw.dbtool.controller;

import com.jackstraw.dbtool.common.Result;
import com.jackstraw.dbtool.pojo.DBConnectData;
import com.jackstraw.dbtool.pojo.TableData;
import com.jackstraw.dbtool.utils.DBToJavaParamTypeMap;
import com.jackstraw.dbtool.utils.DBUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.util.*;

/**
 * 版权所有： 稻草人
 * 创建者： wenhe
 * 创建时间: 2020-01-14
 * 描述：
 * 其它：
 **/
@RestController
@RequestMapping(value="/db")
public class DBController {

    /**
     * 获取数据库连接数据
     * @return
     */
    @RequestMapping(value="/getConnectData")
    public Result<DBConnectData> getConnectData() {
        Result<DBConnectData> result = new Result<>();
        Connection conn = DBUtil.getConn();
        if (conn == null) {
            result.error("尚未连接！");
        } else {
            DBConnectData dbConnectData = DBUtil.getDbConnectData();
            result.setData(dbConnectData);
            result.success();
        }
        return result;
    }

    /**
     * 初始化连接数据库
     * @param type
     * @param url
     * @param port
     * @param dbName
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value="/initConnect")
    public Result<Object> initConnect(String type, String url, String port, String dbName, String username, String password) {
        Result<Object> result = new Result<>();
        try {
            DBUtil.initConnection(type, url, port, dbName, username, password);
            DBToJavaParamTypeMap.initTransMap(type);
            result.success();
        } catch (Exception e) {
            e.printStackTrace();
            result.error(e.getMessage());
        }
        return result;
    }

    /**
     * 获取数据库表列表
     * @return
     */
    @RequestMapping(value="/getTableList")
    public Result<List<TableData>> getTableList() {
        Result<List<TableData>> result = new Result<>();
        try {
            List<TableData> tableList = DBUtil.getTableList();
            result.setData(tableList);
            result.success();
        } catch (Exception e) {
            e.printStackTrace();
            result.error(e.getMessage());
        }
        return result;
    }

    /**
     * 获取数据库字段列表
     * @param tableName
     * @return
     */
    @RequestMapping(value="/getFieldList")
    public Result<List<Map<String, String>>> getFieldList(String tableName) {
        Result<List<Map<String, String>>> result = new Result<>();
        try {
            Map<String, String> columnToRemarkMap = DBUtil.getColumnToRemarkMap(tableName);
            Iterator<Map.Entry<String, String>> iterator = columnToRemarkMap.entrySet().iterator();
            List<Map<String, String>> list = new ArrayList<>();
            while (iterator.hasNext()) {
                Map.Entry<String, String> next = iterator.next();
                String field = next.getKey();
                String remark = next.getValue();
                Map<String, String> map = new HashMap<>();
                map.put("field", field);
                map.put("remark", remark);
                list.add(map);
            }
            result.setData(list);
            result.success();
        } catch (Exception e) {
            e.printStackTrace();
            result.error(e.getMessage());
        }
        return result;
    }
}
